package mil.af.amc.adpetracker.web.actions.struts1.Ros;

import mil.af.amc.adpetracker.web.actions.struts1.AbstractAction;
import mil.af.amc.adpetracker.service.RosService;
import mil.af.amc.adpetracker.service.ServiceException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMessage;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Action to close a ROS/Waiver package
 *
 * @author TSgt Jason Ferguson
 */

public class CloseRosAction extends AbstractAction {

    private static final Log log = LogFactory.getLog(CloseRosAction.class);

    private RosService rosService;

    public void setRosService(RosService rosService) {
        this.rosService = rosService;
    }

    @Override
    public ActionForward execute(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {

        int id = Integer.parseInt(httpServletRequest.getParameter("rosPackageId"));

        try {
            rosService.closeRos(id, new java.sql.Date((new java.util.Date()).getTime()));
        } catch (ServiceException e) {

            log.debug("rosService.closeRos() threw ServiceException." ,e);

            ActionMessage message = new ActionMessage(e.getKey());
            saveError(httpServletRequest, message);
        }

        return actionMapping.findForward("success");
    }
}
